package cn.zjgem.real.report.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.db.PageResult;
import cn.zjgem.real.report.dto.*;
import cn.zjgem.real.report.enums.AppHttpCodeEnum;
import cn.zjgem.real.report.mapper.PublishMapper;
import cn.zjgem.real.report.pojo.Publish;
import cn.zjgem.real.report.service.PublishService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class PublishServiceImpl extends ServiceImpl<PublishMapper, Publish> implements PublishService {
    @Autowired
    private PublishMapper publishMapper;

    @Override
    public ResponseResult savePublish(PublishDto dto) {
        //1.参数检查
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Publish publish = new Publish();
        publish.setUserId(12);
        publish.setMobile("15838474246");
        publish.setStatus(0);
        publish.setText(dto.getText());
        publish.setLocation(dto.getLocation());
        publish.setLon(dto.getLon());
        publish.setLat(dto.getLat());
        Date currentTime = new Date();
        publish.setCreated(currentTime);
        publish.setImgUrls(dto.getImgUrls());
        save(publish);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult publishListById(PageRequestDto dto) {
        //1.参数检查
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //分页参数检查
        dto.checkParam();
        //2.按照名称模糊分页查询
        Page page = new Page(dto.getCurrent(), dto.getSize());
        QueryWrapper<Publish> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", 12);
        IPage result = page(page, wrapper);
        //3.封装结果
        ResponseResult responseResult = new PageResponseResult(dto.getCurrent(), dto.getSize(), (int) result.getTotal());
        responseResult.setData(result.getRecords());
        System.out.println("1");
        return responseResult;
    }

}
